package com.fsc.fragment;

import java.util.Calendar;

import com.fsc.fragment.outcome.OutCome;
import com.fsc.fragment.outcome.OutComeDao;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AddOutComeActivity extends Activity {
	protected static final int DATE_DIALOG_ID = 0;

	private TextView tv_title;
	
	private EditText et_money,et_time,et_address,et_mark;
	private Spinner sp_time;
	private Button btn_cancel,btn_save;
	private int mYear,mMonth,mDay;
	//数据id
	private int id;
	private Intent objIntent;

	OutComeDao outComeDao = new OutComeDao(AddOutComeActivity.this);
	OutCome objOutCome = new OutCome();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_out_come);
		
		initView();
		
		objIntent=getIntent();
		int operate = objIntent.getIntExtra("operate",-1);//取operate判断是新增还是编辑
		
		//给时间控件设置监听
		et_time.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);//显示为日期选择对话框
			}
		});
		
		final Calendar c = Calendar.getInstance();//获取当前系统日期
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		updateDisplay();//显示系统当前时间
		
		//给btn_cancel设置监听
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		if(operate==3){//新增
			btn_save.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String money = et_money.getText().toString();
					if(!money.isEmpty()){
						objIntent = new Intent();
						objIntent.putExtra("money",et_money.getText().toString());
						objIntent.putExtra("time", et_time.getText().toString());
						objIntent.putExtra("address", et_address.getText().toString());
						objIntent.putExtra("type", sp_time.getSelectedItem().toString());
						objIntent.putExtra("mark", et_mark.getText().toString());
						setResult(3, objIntent);
						finish();
					}else{
						Toast.makeText(AddOutComeActivity.this,"请输入支出金额", Toast.LENGTH_LONG).show();
					}
				}
			});
		}else if(operate==1){
			tv_title.setText("管理支出");
			btn_save.setText("编辑");
			et_time.setText("");
			id=objIntent.getIntExtra("id",1);			

			et_money.setText(objIntent.getStringExtra("money"));
			et_time.setText(objIntent.getStringExtra("time"));
			String LX = objIntent.getStringExtra("sp_type");
			if(LX.equals("工资")){
				sp_time.setSelection(0,true);
			}else if(LX.equals("转账")){
				sp_time.setSelection(1,true);
			}else if(LX.equals("红包")){
				sp_time.setSelection(2,true);
			}else if(LX.equals("现金")){
				sp_time.setSelection(3,true);
			}
			et_address.setText(objIntent.getStringExtra("address"));
			et_mark.setText(objIntent.getStringExtra("mark"));
			
			//重新设置监听
			btn_save.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					objOutCome.setMoney(Double.parseDouble(et_money.getText().toString()));
					objOutCome.setTime(et_time.getText().toString());
					objOutCome.setType(sp_time.getSelectedItem().toString());
					objOutCome.setAddress(et_address.getText().toString());
					objOutCome.setMark(et_mark.getText().toString());
					objOutCome.setId(id);
					outComeDao.update(objOutCome);
					
					//Toast.makeText(this, "修改成功！！", Toast.LENGTH_SHORT).show();
					setResult(1, objIntent = getIntent());
					finish();
				}
			});
		}
	}
	//初始化
	private void initView() {
		tv_title = (TextView) findViewById(R.id.tv_title);
		et_money = (EditText) findViewById(R.id.et_money);
		et_time = (EditText) findViewById(R.id.et_time);
		et_address = (EditText) findViewById(R.id.et_address);
		et_mark = (EditText) findViewById(R.id.et_mark);
		sp_time = (Spinner) findViewById(R.id.sp_time);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_save = (Button) findViewById(R.id.btn_save);
	}
	private void updateDisplay() {
		et_time.setText(new StringBuilder().append(mYear).append("-").append(mMonth+1)
				.append("-").append(mDay));
	}
	
	//重写onCreateDialog
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) 
		{
			case DATE_DIALOG_ID:
			  return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
		}
		return null;
	}
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};
}
